﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class DAONhaCungCap:DataProvider
    {
        public static DataTable getAll()
        {
            string select = "select * from nhacungcap";
            var da = new SqlDataAdapter(select, KetNoi());
            DataTable dt = new DataTable();
            da.Fill(dt);
            DongKetNoi();
            return dt;
        }

        public static int add(DTONhaCungCap ncc)
        {
            string select = "select * from nhacungcap";
            var da = new SqlDataAdapter(select, KetNoi());
            new SqlCommandBuilder(da);
            DataTable dt = new DataTable();
            da.FillSchema(dt, SchemaType.Source);
            var row = dt.NewRow();
            row["TenNCC"] = ncc.TenNCC;
            row["DiaChi"] = ncc.DiaChi;
            row["SoDienThoai"] = ncc.SoDienThoai;
            dt.Rows.Add(row);            
            DongKetNoi();
            return da.Update(dt);
        }

        public static int edit(DTONhaCungCap ncc)
        {
            string select = "select * from nhacungcap";
            var da = new SqlDataAdapter(select, KetNoi());
            new SqlCommandBuilder(da);
            DataTable dt = new DataTable();
            da.FillSchema(dt, SchemaType.Source);
            da.Fill(dt);

            var row = dt.Rows.Find(ncc.MaNCC);
            row.BeginEdit();
            row["TenNCC"] = ncc.TenNCC;
            row["DiaChi"] = ncc.DiaChi;
            row["SoDienThoai"] = ncc.SoDienThoai;
            row.EndEdit();
            
            DongKetNoi();
            return da.Update(dt);
        }

        public static int del(DTONhaCungCap ncc)
        {
            string select = "select * from nhacungcap";
            var da = new SqlDataAdapter(select, KetNoi());
            new SqlCommandBuilder(da);
            DataTable dt = new DataTable();
            da.FillSchema(dt, SchemaType.Source);
            da.Fill(dt);

            var row = dt.Rows.Find(ncc.MaNCC);
            row.Delete();            

            DongKetNoi();
            return da.Update(dt);
        }
    }
}
